home *** CD-ROM | disk | FTP | other *** search
/ synchro.net / synchro.net.tar / synchro.net / main / GAMES / cs2d.exe / {app} / bots / Standard AI.lua < prev   
Encoding:
Text File  |  2010-08-25  |  14.6 KB  |  449 lines

  1. --------------------------------------------------
  2. -- CS2D Standard Bot AI                         --
  3. -- 01.08.2010 - www.UnrealSoftware.de           --
  4. --                                              --
  5. -- includes: includes/*                         --
  6. --                                              --
  7. -- Used names in this script                    --
  8. -- vai_ = AI variable                           --
  9. -- fai_ = AI helper function                    --
  10. -- ai_ = AI function (native CS2D Lua AI)       --
  11. --                                              --
  12. -- sys/lua/info.txt contains an AI commandlist! --
  13. --------------------------------------------------
  14.  
  15. -- Includes
  16. dofile("bots/includes/settings.lua")    -- track settings
  17. dofile("bots/includes/general.lua")        -- general helper functions
  18. dofile("bots/includes/buy.lua")            -- buying
  19. dofile("bots/includes/decide.lua")        -- decision making process
  20. dofile("bots/includes/engage.lua")        -- engage/attack/battle
  21. dofile("bots/includes/collect.lua")        -- item collecting
  22.  
  23. -- AI Variables (vai_)
  24. vai_set_gm=0                    -- Game Mode Setting
  25. vai_set_botskill=0                -- Bot Skill Setting
  26. vai_set_botweapons=0            -- Bot Weapons Setting
  27. fai_update_settings()
  28.  
  29. vai_mode={}; vai_smode={}        -- current mode / sub-mode
  30. vai_timer={}                    -- timer
  31. vai_destx={}; vai_desty={}        -- destination x|y
  32. vai_aimx={}; vai_aimy={}        -- aim at x|y
  33. vai_px={}; vai_py={}            -- previous x|y
  34. vai_target={}                    -- target
  35. vai_reaim={}; vai_rescan={}        -- re-aim / re-scan (freeline)
  36. vai_itemscan={}                    -- itemscan countdown
  37. vai_buyingdone={}                -- buying done?
  38. vai_radioanswer={}                -- radio answer?
  39. vai_radioanswert={}                -- radio answer timer
  40. for i=1,32 do
  41.     vai_mode[i]=-1; vai_smode[i]=0; vai_timer[i]=0; vai_destx[i]=0; vai_desty[i]=0; vai_aimx[i]=0; vai_aimy[i]=0;
  42.     vai_px[i]=0; vai_px[i]=0; vai_target[i]=0; vai_reaim[i]=0; vai_rescan[i]=0; vai_itemscan[i]=0; vai_buyingdone[i]=0;
  43.     vai_radioanswer[i]=0; vai_radioanswert[i]=0;
  44. end
  45.  
  46. -- "ai_onspawn" - AI On Spawn Function
  47. -- This function is called by CS2D automatically after each spawn of a bot
  48. -- Parameter: id = player ID of the bot
  49. function ai_onspawn(id)
  50.     -- reload settings
  51.     fai_update_settings()
  52.     -- reset variables
  53.     vai_mode[id]=-1; vai_smode[id]=0
  54.     vai_timer[id]=math.random(1,10)
  55.     vai_destx[id]=0; vai_desty[id]=0
  56.     vai_aimx[id]=player(id,"x")-50+math.random(0,100)
  57.     vai_aimy[id]=player(id,"y")-50+math.random(0,100)
  58.     vai_px[id]=player(id,"x")
  59.     vai_py[id]=player(id,"y")
  60.     vai_target[id]=0
  61.     vai_reaim[id]=0; vai_rescan[id]=0
  62.     vai_itemscan[id]=1000
  63.     vai_buyingdone[id]=0
  64.     vai_radioanswer[id]=0; vai_radioanswert[id]=0;
  65. end
  66.  
  67. -- "ai_update_living" - AI Update Living Function
  68. -- This function is called by CS2D automatically for each *LIVING* bot each frame
  69. -- Parameter: id = player ID of the bot
  70. function ai_update_living(id)
  71.         
  72.     -- Engage / Aim
  73.     fai_engage(id)
  74.     
  75.     -- Radio Answer
  76.     if vai_radioanswert[id]>0 then
  77.         vai_radioanswert[id]=vai_radioanswert[id]-1
  78.         if vai_radioanswert[id]<=0 then
  79.             ai_radio(id,vai_radioanswer[id])
  80.             vai_radioanswer[id]=0; vai_radioanswert[id]=0
  81.         end
  82.     end
  83.     
  84.     if player(id,"health")>0 and player(id,"team")>0 then
  85.         
  86.         -- Collect Items
  87.         fai_collect(id)
  88.         
  89.         if player(id,"health")>0 and player(id,"team")>0 then
  90.             
  91.             ai_debug(id,"m:"..vai_mode[id]..", sm:"..vai_smode[id].." t:"..vai_target[id])
  92.             
  93.             if vai_mode[id]==0 then
  94.                 -- 0: IDLE ------------------------------------------------> decide what to do next
  95.                 vai_timer[id]=0; vai_smode[id]=0
  96.                 fai_decide(id)
  97.                 
  98.             elseif vai_mode[id]==1 then
  99.                 -- 1: CAMP ------------------------------------------------> do nothing
  100.                 fai_wait(id,0)
  101.                 
  102.             elseif vai_mode[id]==2 then
  103.                 -- 2: GOTO ------------------------------------------------> go to destination
  104.                 local result=ai_goto(id,vai_destx[id],vai_desty[id])
  105.                 if result==1 then
  106.                     vai_mode[id]=0
  107.                 elseif result==0 then
  108.                     vai_mode[id]=0
  109.                 else
  110.                     fai_walkaim(id)
  111.                 end
  112.                 
  113.             elseif vai_mode[id]==3 then
  114.                 -- 3: ROAM ------------------------------------------------> randomly run round
  115.                 if ai_move(id,vai_smode[id])==0 then
  116.                     -- Bot failed to walk (way blocked) -> turn
  117.                     if (id%2)==0 then
  118.                         vai_smode[id]=vai_smode[id]+45
  119.                     else
  120.                         vai_smode[id]=vai_smode[id]-45
  121.                     end
  122.                     vai_timer[id]=math.random(150,250)
  123.                 end
  124.                 fai_walkaim(id)
  125.                 fai_wait(id,0)
  126.  
  127.             elseif vai_mode[id]==4 then
  128.                 -- 4: FIGHT -----------------------------------------------> fight
  129.                 if player(vai_target[id],"exists") then
  130.                     if player(vai_target[id],"health")>0 then
  131.                         -- Melee Combat?
  132.                         if itemtype(player(id,"weapontype"),"range")<50 then
  133.                             -- Yes, melee! Run to target
  134.                             if ai_goto(id,player(vai_target[id],"tilex"),player(vai_target[id],"tiley"))~=2 then
  135.                                 vai_mode[id]=0
  136.                             end
  137.                         else
  138.                             -- No, regular combat!
  139.                             vai_timer[id]=vai_timer[id]-1
  140.                             if vai_timer[id]<=0 then
  141.                                 vai_timer[id]=math.random(50,150)
  142.                                 vai_smode[id]=math.random(0,360)
  143.                                 -- Hunt?
  144.                                 if math.random(1,2)==1 then
  145.                                     if player(id,"health")>50 then
  146.                                         if math.abs(player(id,"x")-player(vai_target[id],"x"))>230 and math.abs(player(id,"y")-player(vai_target[id],"y"))>180 then
  147.                                             vai_mode[id]=5
  148.                                             vai_smode[id]=vai_target[id]
  149.                                         end
  150.                                     end
  151.                                 end
  152.                             end
  153.                             if ai_move(id,vai_smode[id])==0 then
  154.                                 -- Bot failed to walk (way blocked) -> turn
  155.                                 if (id%2)==0 then
  156.                                     vai_smode[id]=vai_smode[id]+45
  157.                                 else
  158.                                     vai_smode[id]=vai_smode[id]-45
  159.                                 end
  160.                                 vai_timer[id]=math.random(50,150)
  161.                             end
  162.                         end
  163.                         return
  164.                     end
  165.                 end
  166.                 -- End Fight
  167.                 vai_mode[id]=0
  168.  
  169.             elseif vai_mode[id]==5 then
  170.                 -- 5: HUNT -----------------------------------------------> hunt
  171.                 if player(vai_smode[id],"exists") then
  172.                     if player(vai_smode[id],"health")>0 then
  173.                         if ai_goto(id,player(vai_smode[id],"tilex"),player(vai_smode[id],"tiley"))~=2 then
  174.                             vai_mode[id]=0
  175.                         end
  176.                         return
  177.                     end
  178.                 end
  179.                 -- End Hunt
  180.                 vai_mode[id]=0
  181.                 
  182.             elseif vai_mode[id]==6 then
  183.                 -- 6: COLLECT --------------------------------------------> collect
  184.                 if ai_goto(id,vai_destx[id],vai_desty[id])~=2 then
  185.                     vai_mode[id]=0
  186.                     vai_itemscan[id]=140
  187.                 else
  188.                     fai_walkaim(id)
  189.                 end
  190.                 
  191.             elseif vai_mode[id]==7 then
  192.                 -- 7: FOLLOW -----------------------------------------------> follow
  193.                 if player(vai_smode[id],"exists") then
  194.                     if player(vai_smode[id],"health")>0 then
  195.                         ai_goto(id,player(vai_smode[id],"tilex"),player(vai_smode[id],"tiley"))
  196.                         fai_walkaim(id)
  197.                         return
  198.                     end
  199.                 end
  200.                 -- End Follow
  201.                 vai_mode[id]=0
  202.                 
  203.             
  204.             elseif vai_mode[id]==50 then
  205.                 -- 50: RESCUE ---------------------------------------------> rescue hostages
  206.                 if vai_smode[id]==0 then
  207.                     -- Find and use hostages
  208.                     if ai_goto(id,vai_destx[id],vai_desty[id])~=2 then
  209.                         vai_mode[id]=0
  210.                     else
  211.                         fai_walkaim(id)
  212.                     end
  213.                     -- Find Hostages
  214.                     local h=hostage(0,"table")
  215.                     for i=1,#h do
  216.                         if hostage(h[i],"health")>0 and hostage(h[i],"follow")==0 then
  217.                             -- Close enough? Use!
  218.                             if math.abs(player(id,"x")-hostage(h[i],"x"))<=15 and math.abs(player(id,"y")-hostage(h[i],"y"))<=15 then
  219.                                 ai_rotate(id,fai_angleto(player(id,"x"),player(id,"y"),hostage(h[i],"x"),hostage(h[i],"y")))
  220.                                 ai_use(id)
  221.                                 break
  222.                             end
  223.                         end
  224.                     end
  225.                     -- Get closest Hostage
  226.                     vai_destx[id],vai_desty[id]=closehostage(id)
  227.                     if vai_destx[id]==-100 then
  228.                         -- None found? Switch to rescue
  229.                         vai_smode[id]=1
  230.                         vai_destx[id],vai_desty[id]=randomentity(4) -- info_rescuepoint
  231.                         if vai_destx[id]==-100 then
  232.                             vai_destx[id],vai_desty[id]=randomentity(1) -- info_ct
  233.                         end
  234.                     end
  235.                 else
  236.                     -- Return and rescue hostages
  237.                     local result=ai_goto(id,vai_destx[id],vai_desty[id])
  238.                     if result==1 then
  239.                         vai_mode[id]=3
  240.                         vai_timer[id]=math.random(150,300)
  241.                         vai_smode[id]=math.random(0,360)
  242.                     elseif result==0 then
  243.                         vai_mode[id]=0
  244.                     else
  245.                         fai_walkaim(id)
  246.                     end
  247.                 end
  248.  
  249.             elseif vai_mode[id]==51 then
  250.                 -- 51: PLANT ----------------------------------------------> plant bomb
  251.                 if player(id,"bomb") then
  252.                     -- On bombspot?
  253.                     if tile(player(id,"tilex"),player(id,"tiley"),"entity")~=0 then
  254.                         if inentityzone(player(id,"tilex"),player(id,"tiley"),5) then
  255.                             -- Bomb selected?
  256.                             if player(id,"weapontype")~=55 then
  257.                                 -- Select bomb!
  258.                                 ai_selectweapon(id,55)
  259.                             else
  260.                                 -- Plant
  261.                                 if vai_timer[i]==0 then
  262.                                     ai_radio(id,6) -- cover me!
  263.                                     vai_timer[i]=1
  264.                                 end
  265.                                 ai_attack(id)
  266.                             end
  267.                             return
  268.                         end
  269.                     end
  270.                     -- Not on bombspot -> Goto bombspot!
  271.                     if ai_goto(id,vai_destx[id],vai_desty[id])~=2 then
  272.                         vai_destx[id],vai_desty[id]=randomentity(5) -- info_bombspot
  273.                     else
  274.                         fai_walkaim(id)
  275.                     end
  276.                 else
  277.                     -- Has no bomb anymore! no planting!
  278.                     vai_mode[id]=0
  279.                 end
  280.                 
  281.             elseif vai_mode[id]==52 then
  282.                 -- 52: DEFUSE ---------------------------------------------> defuse bomb
  283.                 if vai_smode[id]==0 then
  284.                     -- Check Bombspot
  285.                     if ai_goto(id,vai_destx[id],vai_desty[id])~=2 then
  286.                         vai_destx[id],vai_desty[id]=randomentity(5,0) -- info_bombspot
  287.                     else
  288.                         fai_walkaim(id)
  289.                     end
  290.                     -- Close to spot? Check
  291.                     if math.abs(player(id,"tilex")-vai_destx[id])<7 and math.abs(player(id,"tiley")-vai_desty[id])<7 then
  292.                         local it=item(0,"table")
  293.                         for i=1,#it do
  294.                             if item(it[i],"type")==63 then
  295.                                 if math.abs(player(id,"tilex")-item(it[i],"x"))<10 and math.abs(player(id,"tiley")-item(it[i],"y"))<10 then
  296.                                     -- Bomb at spot!
  297.                                     vai_destx[id]=item(it[i],"x")
  298.                                     vai_desty[id]=item(it[i],"y")
  299.                                     vai_smode[id]=1
  300.                                     return
  301.                                 end
  302.                             end
  303.                         end
  304.                         -- No bomb at spot!
  305.                         setentityaistate(vai_destx[id],vai_desty[id],1)
  306.                         print "SECTOR CLEAR!"
  307.                         ai_radio(id,5) -- sector clear!
  308.                         local bots=player(0,"table")
  309.                         for i=1,#bots do
  310.                             if player(bots[i],"bot")==1 then
  311.                                 if vai_mode[bots[i]]==52 and vai_destx[bots[i]]==vai_destx[id] and vai_desty[bots[i]]==vai_desty[id] then
  312.                                     vai_destx[bots[i]],vai_desty[bots[i]]=randomentity(5,0)
  313.                                     vai_smode[bots[i]]=0
  314.                                 end
  315.                             end
  316.                         end
  317.                         vai_destx[id],vai_desty[id]=randomentity(5,0) -- info_bombspot
  318.                         return
  319.                     end
  320.                 else
  321.                     -- Defuse Bomb
  322.                     local result=ai_goto(id,vai_destx[id],vai_desty[id])
  323.                     if result==1 then
  324.                         -- Defuse!
  325.                         if vai_timer[i]==0 then
  326.                             ai_radio(id,6) -- cover me!
  327.                             vai_timer[i]=1
  328.                         end
  329.                         ai_use(id)
  330.                     elseif result==0 then
  331.                         -- Failed to reach bomb
  332.                         vai_mode[id]=0
  333.                     end
  334.                 end
  335.             
  336.             elseif vai_mode[id]==-1 then
  337.                 -- -1: BUY ------------------------------------------------> buy equipment
  338.                 fai_buy(id)
  339.             
  340.             else
  341.                 -- INVALID MODE -------------------------------------------> invalid -> select new mode
  342.                 print("invalid AI mode: "..vai_mode[id])
  343.                 vai_mode[id]=0
  344.             end
  345.         
  346.         end
  347.     end
  348. end
  349.  
  350. -- "ai_update_dead" - AI Update Dead Function
  351. -- This function is called by CS2D automatically for each *DEAD* bot each second
  352. -- Parameter: id = player ID of the bot
  353. function ai_update_dead(id)
  354.     -- Try to respawn (if not in normal gamemode)
  355.     if vai_set_gm~=0 then
  356.         ai_respawn(id)
  357.     end
  358. end
  359.  
  360. -- "ai_hear_radio" - AI Hear Radio
  361. -- This function is called once for each radio message
  362. -- Parameter: source = player ID of the player who sent the radio message
  363. -- Parameter: radio = radio message ID
  364. function ai_hear_radio(source,radio)
  365.     -- print("AI HEARD RADIO FROM "..source.." RADIO ID: "..radio)
  366.     -- Bomb planted!
  367.     if radio==4 then
  368.         -- Every CT will try to defuse!
  369.         local bots=player(0,"table")
  370.         for i=1,#bots do
  371.             if player(bots[i],"bot")==1 and player(bots[i],"team")==2 then
  372.                 if vai_mode[bots[i]]~=52 then
  373.                     vai_destx[bots[i]],vai_desty[bots[i]]=randomentity(5,0)
  374.                     vai_mode[bots[i]]=52; vai_smode[bots[i]]=0; vai_timer[bots[i]]=0
  375.                 end
  376.             end
  377.         end
  378.     -- Need Backup / Cover me / Follow Me
  379.     elseif radio==1 or radio==6 or radio==13 then
  380.         local mate=fai_randommate(source)
  381.         if mate~=0 then
  382.             if math.random(1,2)==1 then vai_radioanswer[mate]=0 else vai_radioanswer[mate]=28 end
  383.             vai_radioanswert[mate]=math.random(35,100)
  384.             vai_mode[mate]=7; vai_smode[mate]=source
  385.         end
  386.     -- Enemy spotted / Taking fire, need assistance
  387.     elseif radio==9 or radio==11 then
  388.         local mate=fai_randommate(source)
  389.         if mate~=0 then
  390.             if math.random(1,2)==1 then vai_radioanswer[mate]=0 else vai_radioanswer[mate]=28 end
  391.             vai_radioanswert[mate]=math.random(35,100)
  392.             vai_mode[mate]=2
  393.             vai_destx[mate]=player(source,"tilex")
  394.             vai_desty[mate]=player(source,"tiley")
  395.         end
  396.     -- Regroup Team (stop following)
  397.     elseif radio==24 then
  398.         local team=player(source,"team")
  399.         if team>2 then team=2 end
  400.         local mates=player(0,"team"..team.."living")
  401.         local c=1
  402.         for mate=1,#mates do
  403.             if vai_mode[mate]==7 then
  404.                 if math.random(1,2)==1 then vai_radioanswer[mate]=0 else vai_radioanswer[mate]=28 end
  405.                 vai_radioanswert[mate]=math.random(50,55)*c
  406.                 c=c+1
  407.                 vai_mode[mate]=0
  408.             end
  409.         end
  410.     -- Hold Position
  411.     elseif radio==23 then
  412.         local mate=fai_randommate(source)
  413.         if mate~=0 then
  414.             if math.random(1,2)==1 then vai_radioanswer[mate]=0 else vai_radioanswer[mate]=28 end
  415.             vai_radioanswert[mate]=math.random(35,100)
  416.             vai_mode[mate]=1; vai_timer[mate]=math.random(50*30,50*60)
  417.         end
  418.     -- Team Fall Back / Go Go Go / Stick Together / Storm the Front / You take the point
  419.     elseif radio==10 or radio==15 or radio==30 or radio==31 or radio==32 then
  420.         local team=player(source,"team")
  421.         if team>2 then team=2 end
  422.         local mates=player(0,"team"..team.."living")
  423.         local c=1
  424.         for mate=1,#mates do
  425.             if vai_mode[mate]==1 or vai_mode[mate]==7 then
  426.                 if math.random(1,2)==1 then vai_radioanswer[mate]=0 else vai_radioanswer[mate]=28 end
  427.                 vai_radioanswert[mate]=math.random(50,55)*c
  428.                 c=c+1
  429.                 vai_mode[mate]=0
  430.             end
  431.         end
  432.     -- Report in
  433.     elseif radio==25 then
  434.         local mate=fai_randommate(source)
  435.         if mate~=0 then
  436.             vai_radioanswer[mate]=26
  437.             vai_radioanswert[mate]=math.random(35,100)
  438.         end
  439.     end
  440. end
  441.  
  442. -- "ai_hear_chat" - AI Hear Chat
  443. -- This function is called once for each chat message
  444. -- Parameter: source = player ID of the player who sent the radio message
  445. -- Parameter: msg = chat text message
  446. -- Parameter: teamonly = team only chat message (1) or public chat message (0)
  447. function ai_hear_chat(source,msg,teamonly)
  448.     -- Ignore chat
  449. end